﻿using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Oxytocin
{
    public partial class PrintOxytocin : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        DateTime StatDate;
        DateTime EndDate;
        #endregion

        #region 构造函数
        public PrintOxytocin(Tb_User loginUser, PatientInfo patientInfo, DateTime StatDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.StatDate = StatDate;
            this.EndDate = EndDate;
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\催产素引产记录单.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 获取链接报表事件
        public void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(this.PatientInfo.PATIENT_ID, this.PatientInfo.VISIT_ID, (int)FormType.Oxytocins, StatDate, EndDate);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];
                    OXT ot = XmlUtil.XMLToObject<OXT>(record1.NursingFormData, Encoding.UTF8);
                    dt.Rows.Add(
                        PatientInfo.PATIENT_NAME,   //患者姓名
                        PatientInfo.AGE,   //年龄
                        ot.TaiCi,    //胎次
                        ot.ChanCi,     //产次
                        PatientInfo.BED_NO,    //床号
                        PatientInfo.PATIENT_ID,   //住院号

                        ot.ZhenDuan,     //诊断
                        ot.CuiChanSu,       //催产素
                        ot.JiaCuiChanSu,     //加催产素

                        ot.RiQi,       //日期
                        DateTimeHelper.HHmmssToHHmm(ot.ShiJian),    //时间
                        ot.MeiFenZhongDiShu,    //每分钟滴数

                        ot.GongSuoQiangDu,     //宫缩强度
                        ot.GongSuoChiXu,      //宫缩持续
                        ot.GongSuoJianGe,       //宫缩间隔

                        ot.GongKouWeiZhi,     //宫口位置
                        ot.GongKouZhiDi,    //宫口质地
                        ot.GongKouDaXiao,     //宫口大小

                        ot.XianLuGaoDi,    //先露高低
                        ot.TaiMo,    //胎膜
                        ot.TaiXin,    //胎心

                        ot.XueYa_Zuo,    //血压_低
                        ot.XueYa_You,     //血压_高

                        ot.BeiZhu,      //备注
                        ot.QianMing    //签名
                        );
                }
                dt.DefaultView.Sort = "ShiJian ASC";
                DataTable data = dt.DefaultView.ToTable();
                //将 DataTable 的数据转储到 Grid++Report 的数据集中
                Utility.FillRecordToReport(Report, data);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));    //患者姓名
            dt.Columns.Add("AGE", typeof(string));      //年龄
            dt.Columns.Add("TaiCi", typeof(string));      //胎次
            dt.Columns.Add("ChanCi", typeof(string));   //产次
            dt.Columns.Add("BED_NO", typeof(string));      //床号
            dt.Columns.Add("PATIENT_ID", typeof(string));   //住院号
            
            dt.Columns.Add("ZhenDuan", typeof(string));      //诊断
            dt.Columns.Add("CuiChanSu", typeof(string));   //催产素
            dt.Columns.Add("JiaCuiChanSu", typeof(string));      //加催产素

            dt.Columns.Add("RiQi", typeof(string));      //日期
            dt.Columns.Add("ShiJian", typeof(string));   //时间
            dt.Columns.Add("MeiFenZhongDiShu", typeof(string));   //每分钟滴数

            dt.Columns.Add("GongSuoQiangDu", typeof(string));      //宫缩强度
            dt.Columns.Add("GongSuoChiXu", typeof(string));   //宫缩持续
            dt.Columns.Add("GongSuoJianGe", typeof(string));      //宫缩间隔

            dt.Columns.Add("GongKouWeiZhi", typeof(string));      //宫口位置
            dt.Columns.Add("GongKouZhiDi", typeof(string));   //宫口质地
            dt.Columns.Add("GongKouDaXiao", typeof(string));      //宫口大小

            dt.Columns.Add("XianLuGaoDi", typeof(string));   //先露高低
            dt.Columns.Add("TaiMo", typeof(string));      //胎膜
            dt.Columns.Add("TaiXin", typeof(string));      //胎心

            dt.Columns.Add("XueYa_Zuo", typeof(string));   //血压_低
            dt.Columns.Add("XueYa_You", typeof(string));      //血压_高

            dt.Columns.Add("BeiZhu", typeof(string));      //备注
            dt.Columns.Add("QianMing", typeof(string));   //签名
        }

        #endregion

        #region 窗体加载启动锐浪报表
        private void PrintOxytocin_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
    }
}
